<template>
  <section>
    <!--#region 搜索条-->

    <xt-search @search="handleSearch" :searchFlag="getOperation('list')">
        <el-input v-model="filters.type" placeholder="操作类型" size="small" class="search-box__input" clearable @keyup.enter.native="handleSearch"></el-input>
        <el-input v-model="filters.applicationName" placeholder="服务名称" size="small" class="search-box__input" clearable @keyup.enter.native="handleSearch"></el-input>
        <el-input v-model="filters.subject" placeholder="模块" size="small" class="search-box__input" clearable @keyup.enter.native="handleSearch"></el-input>
        <el-input v-model="filters.userName" placeholder="操作人" size="small" class="search-box__input" clearable @keyup.enter.native="handleSearch"></el-input>
        <el-date-picker v-model="filters.startDate" type="date" placeholder="开始时间" size="small" :editable="false" class="search-box__input" @change="handleSearch"></el-date-picker>
        <el-date-picker v-model="filters.endDate" type="date" placeholder="结束时间" size="small" :editable="false" class="search-box__input" @change="handleSearch"></el-date-picker>
        <template slot="actions">
        </template>
      </xt-search>

    <!--#endregion-->

    <!--#region 页面主要列表-->

    <!--列表-->

    <el-table :data="logList" highlight-current-row v-loading="loading.listLoading" style="width: 100%;" stripe border>
      <el-table-column align="left" header-align="center" type="index" width="56" label="序号">
        <template slot-scope="scope">
          {{(pageInfo.pageIndex-1)*pageInfo.pageSize+scope.$index+1}}
        </template>
      </el-table-column>
      <el-table-column align="left" header-align="center" prop="type" label="操作类型" :show-overflow-tooltip="true" width="80"></el-table-column>
      <el-table-column align="left" header-align="center" prop="applicationName" label="服务" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="subject" label="模块" :show-overflow-tooltip="true" width="80"></el-table-column>
      <el-table-column align="left" header-align="center" prop="iP" label="IP地址" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="requestUri" label="请求接口" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="requestDate" label="请求时间" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column align="left" header-align="center" prop="content" label="请求参数" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="userName" label="操作人" :show-overflow-tooltip="true" width="90"></el-table-column>
      <el-table-column align="left" header-align="center" prop="requestTime" label="耗时" :show-overflow-tooltip="true" width="80"></el-table-column>
      <el-table-column align="left" header-align="center" prop="exception" label="异常信息" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="remark" label="接口描述信息" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" header-align="center" prop="createDate" label="记录时间" :show-overflow-tooltip="true" width="160"></el-table-column>
    </el-table>
    <!--#endregion-->

    <!--#region 工具条-->

    <!--工具条-->

    <el-row>
      <el-col :span="24" class="toolbar">
        <el-pagination
          style="float:right;"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.pageIndex"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageInfo.pageSize"
          layout="sizes, prev, pager, next"
          :total="pageInfo.count">
        </el-pagination>
      </el-col>
    </el-row>

    <!--#endregion-->

    <!--#region 新增修改页面-->

    <!--新增界面-->

    <el-dialog width="95%" top="20px" :title="formData.id===0?'新增':'修改'" :visible.sync="dialog.formDataVisible" :close-on-click-modal="false" :modal-append-to-body="false" append-to-body>
      <el-form :model="formData" label-width="100px" :rules="formRules" ref="formData">
        <el-form-item label="操作类型" prop="type">
          <el-input v-model="formData.type" auto-complete="off" :maxlength="50" placeholder="请输入操作类型" size="small"></el-input>
        </el-form-item>
        <el-form-item label="服务名称" prop="applicationName">
          <el-input v-model="formData.applicationName" auto-complete="off" :maxlength="50" placeholder="请输入服务名称" size="small"></el-input>
        </el-form-item>
        <el-form-item label="模块" prop="subject">
          <el-input v-model="formData.subject" auto-complete="off" :maxlength="50" placeholder="请输入模块" size="small"></el-input>
        </el-form-item>
        <el-form-item label="参数内容" prop="content">
          <el-input type="textarea" v-model="formData.content" auto-complete="off" :maxlength="65535" placeholder="请输入参数内容" size="small"></el-input>
        </el-form-item>
        <el-form-item label="ip" prop="iP">
          <el-input v-model="formData.iP" auto-complete="off" :maxlength="50" placeholder="请输入ip" size="small"></el-input>
        </el-form-item>
        <el-form-item label="userId" prop="userId">
            <xt-input-number type="number" v-model.number="formData.userId" auto-complete="off" :maxlength="0" placeholder="请输入" size="small" style="width:100%"></xt-input-number>
        </el-form-item>
        <el-form-item label="userName" prop="userName">
          <el-input v-model="formData.userName" auto-complete="off" :maxlength="50" placeholder="请输入" size="small"></el-input>
        </el-form-item>
        <el-form-item label="请求api" prop="requestUri">
          <el-input v-model="formData.requestUri" auto-complete="off" :maxlength="100" placeholder="请输入请求api" size="small"></el-input>
        </el-form-item>
        <el-form-item label="请求时间" prop="requestDate">
          <el-date-picker v-model="formData.requestDate" type="date" placeholder="请选择请求时间" size="small" :editable="false" style="width:100%"></el-date-picker>
        </el-form-item>
        <el-form-item label="异常信息" prop="exception">
          <el-input v-model="formData.exception" auto-complete="off" :maxlength="300" placeholder="请输入异常信息" size="small"></el-input>
        </el-form-item>
        <el-form-item label="接口描述信息" prop="remark">
          <el-input type="textarea" resize="none" v-model="formData.remark" auto-complete="off" :maxlength="255" placeholder="请输入接口描述信息" size="small"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel">取消</el-button>
        <el-button type="primary" @click.native="submitData" :loading="loading.addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--#endregion-->

  </section>
</template>

<script>

/*#region 导入函数*/

import PagePlugIn from "../../mixins/page-plug-in";
import {LogAPI} from "../../modules";

/*#endregion*/

export default {
  name: "Log",
  methods: {

    /*#region 自定义函数(组件change事件等)*/

    /*#endregion*/

    /*#region 数据提交事件*/

    //提交事件

    submitData() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.formData));
          if (params.id === 0) {
            this.loading.addLoading = true;
            LogAPI.insertLog(params).then(({data}) => {
              this.loading.addLoading = false;
              if (data.code === 1) {
                this.$refs.formData.resetFields();
                this.getMainList();
                this.dialog.formDataVisible = false;
              } else {
                this.$message({
                  message: data.msg || "提交失败！",
                  type: 'error'
                });
              }
            });
        } else {
          this.loading.addLoading = true;
          LogAPI.updateLog(params).then(({data}) => {
            this.loading.addLoading = false;
            if (data.code === 1) {
              this.$refs.formData.resetFields();
              this.getMainList();
              this.dialog.formDataVisible = false;
            } else {
              this.$message({
                message: data.msg || "提交失败！",
                type: 'error'
              });
            }
          });
          }
        }
      });
    },

    //删除

    deleteData(row) {
      this.$confirm("删除之后将无法恢复, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        LogAPI.deleteLog({id: row.id}).then(({data}) => {
          if (data.code === 1) {
            this.getMainList();
          }
        });
      });
    },


    /*#endregion*/

    /*#region 操作函数(handle)*/

    //修改

    handleEdit(row) {
      this.$refs.formData && this.$refs.formData.resetFields();
      this.formData = JSON.parse(JSON.stringify(row));
      this.dialog.formDataVisible = true;
    },
    handleCancel() {
      this.dialog.formDataVisible = false;
    },
    handleAdd() {
      this.$refs.formData && this.$refs.formData.resetFields();
      this.formData = LogAPI.init();
      this.dialog.formDataVisible = true;
    },

    /*#endregion*/

    /*#region 获取数据*/

    getMainList() {
      const params = {
        ...this.getParams()
      };
      this.loading.listLoading = true;
      LogAPI.listLog(params).then(({data, list}) => {
        this.loading.listLoading = false;
        this.logList = list;
        this.pageInfo.pageIndex = data.data ? data.data.currentPage : 1;
        this.pageInfo.count = data.data ? data.data.count : 0;
      });
    }

    /*#endregion*/

  },

  /*#region 生命周期函数(created、mount、等)*/

  created() {
    this.getMainList();
  },

  /*#endregion*/

  /*#region 注册绑定数据*/

  props: {},
  components: {},
  watch: {},
  mixins: [PagePlugIn],
  data() {
    return {
      filters: LogAPI.init(),
      formData: LogAPI.init(),
      formRules: {
      },
      dialog: { formDataVisible: false },
      loading: { addLoading: false, listLoading: false },
      logList: []
    };
  }

  /*#endregion */

};
</script>

<style scoped lang="scss">
@import "@src/assets/styles/scss/views/log/log.scss";
</style>
